<?php

namespace App\Models\Goods;

use App\Models\Base;
use Illuminate\Support\Facades\DB;

class GoodsSku extends Base
{
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'goods_sku';

    public static function getGoodsSkuBySkuSpeciValueId($speci_value_ids,$id){
        $goods_skus =  self::where(['speci_value_ids'=>$speci_value_ids,'status'=>1,'goods_id'=>$id])->first()->toArray();
        return $goods_skus ?? [];
    }

    public static function getGoodsAndGoodsSkuById($id){
        $res = self::find($id);
//        echo $res->goods;
        return $res;

    }

    public function goods()
    {
        return $this->belongsTo('App\Models\Goods\Goods');
    }

    public  function getSkuInfoByIds($ids){
        return $this->getInfoByIds($ids);
    }

    public static function updateNum($datas){
        foreach ($datas as $data){
            DB::table('goods_sku')->where(['id'=>$data['sku_id']])->decrement('stock', $data['num']);
        }
        return true;
    }

}
